|
|
|
|
|
|
|
Listing 28.1 ADOTEST.ZIPCreating an ADO Recordset |
|
|
|
|
|
|
|
|
Const sSQL = "SELECT * FROM Authors Where [Author] Like 'Q%'"
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
'OPEN A CONNECTION
Set cn = New ADODB.Connection
cn.Open "DSN=BIBLIO"
'OPEN A RECORDSET
Set rs = New ADODB.Recordset
rs.ActiveConnection = cn
rs.Source = sSQL
rs.Open
'PRINT THE AUTHORS' NAMES
rs.MoveFirst
While Not rs.EOF
Debug.Print "author = " & rs.Fields("Author")
rs.MoveNext
Wend
'CLOSE EVERYTHING
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing |
|
|
|
|
|
|
|
|
The code in Listing 28.1 runs a SQL query that selects all the authors who have last names beginning with the letter Q. |
|
|
|
|
|
|
|
|
|
The Like syntax of the SQL query in Listing 28.1 is specific to a Microsoft Access database. In a SQL Server environment, you might write the same statement as follows: |
|
|
|
|
|
|
|
|
Select * from Authors where Author like "Q*"  |
|
|
|
|
|
|
|
|
|
|
To access data in the Recordset object, use the Fields collection. Table 28.2 lists the options you can use to refer to an individual field. |
|
|
|
|
| Table 28.2 Working with Field Values | | Syntax | Example | | Recordset.fields(field name) | rsPeople.Fields("LastName") | | Recordset.fields(index) | rsPeople.Fields(2) | | Recordset!field name | rsPeople!LastName |
|
|
|